package com.hig.risk.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hig.risk.mapper.RiskInspectionFileMapper;
import com.hig.risk.domain.RiskInspectionFile;
import com.hig.risk.service.IRiskInspectionFileService;

/**
 * 自查来源文件Service业务层处理
 * 
 * @author scp
 * @date 2024-10-08
 */
@Service
public class RiskInspectionFileServiceImpl implements IRiskInspectionFileService 
{
    @Autowired
    private RiskInspectionFileMapper riskInspectionFileMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询自查来源文件
     * 
     * @param id 自查来源文件ID
     * @return 自查来源文件
     */
    @Override
    public RiskInspectionFile selectRiskInspectionFileById(Long id)
    {
        return riskInspectionFileMapper.selectRiskInspectionFileById(id);
    }

    /**
     * 查询自查来源文件列表
     * 
     * @param riskInspectionFile 自查来源文件
     * @return 自查来源文件
     */
    @Override
    public List<RiskInspectionFile> selectRiskInspectionFileList(RiskInspectionFile riskInspectionFile)
    {
        return riskInspectionFileMapper.selectRiskInspectionFileList(riskInspectionFile);
    }

    /**
     * 新增自查来源文件
     * 
     * @param riskInspectionFile 自查来源文件
     * @return 结果
     */
    @Override
    public int insertRiskInspectionFile(RiskInspectionFile riskInspectionFile)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        riskInspectionFile.setCreateBy(loginUser.getUser().getUserName());
        riskInspectionFile.setDeptId(loginUser.getUser().getDeptId());
        riskInspectionFile.setCreateTime(DateUtils.getNowDate());
        String fileUrl = riskInspectionFile.getFileUrl();
        String[] fileUrls = fileUrl.split(",");
        String fileName = "";
        for (String url : fileUrls){
            int lastIndex = url.lastIndexOf("/");
            if (lastIndex != -1) {
                String name = url.substring(lastIndex + 1);
                fileName += name + ",";
            }
        }
        if (fileName.length()>0){
            fileName = fileName.substring(0,fileName.length()-1);
        }
        riskInspectionFile.setFileName(fileName);
        return riskInspectionFileMapper.insertRiskInspectionFile(riskInspectionFile);
    }

    /**
     * 修改自查来源文件
     * 
     * @param riskInspectionFile 自查来源文件
     * @return 结果
     */
    @Override
    public int updateRiskInspectionFile(RiskInspectionFile riskInspectionFile)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        riskInspectionFile.setUpdateBy(loginUser.getUser().getUserName());
        riskInspectionFile.setUpdateTime(DateUtils.getNowDate());
        String fileUrl = riskInspectionFile.getFileUrl();
        String[] fileUrls = fileUrl.split(",");
        String fileName = "";
        for (String url : fileUrls){
            int lastIndex = url.lastIndexOf("/");
            if (lastIndex != -1) {
                String name = url.substring(lastIndex + 1);
                fileName += name + ",";
            }
        }
        if (fileName.length()>0){
            fileName = fileName.substring(0,fileName.length()-1);
        }
        riskInspectionFile.setFileName(fileName);
        return riskInspectionFileMapper.updateRiskInspectionFile(riskInspectionFile);
    }

    /**
     * 批量删除自查来源文件
     * 
     * @param ids 需要删除的自查来源文件ID
     * @return 结果
     */
    @Override
    public int deleteRiskInspectionFileByIds(Long[] ids)
    {
        return riskInspectionFileMapper.deleteRiskInspectionFileByIds(ids);
    }

    /**
     * 删除自查来源文件信息
     * 
     * @param id 自查来源文件ID
     * @return 结果
     */
    @Override
    public int deleteRiskInspectionFileById(Long id)
    {
        return riskInspectionFileMapper.deleteRiskInspectionFileById(id);
    }

    @Override
    public List<SysDictData> selectOptionList(RiskInspectionFile riskInspectionFile) {
        return riskInspectionFileMapper.selectOptionList(riskInspectionFile);
    }
}
